# Copyright (c) 2023, json_wang and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
import json


class CreatePurchaseReceipt(Document):
    pass

    @frappe.whitelist()
    def bt_create(self):
        print("点击了按钮")
        print(self)

@frappe.whitelist()
def create_order(source_doc):
    print("调用了form上的按钮")
    # 创建采购入库单
    if isinstance(source_doc, str):
        source_doc = frappe._dict(json.loads(source_doc))
    purchase_receipt = frappe.new_doc('Purchase Receipt')
    purchase_receipt.company = source_doc.company
    purchase_receipt.supplier = '山水电子'  # 设置供应商
    purchase_receipt.append('items', {
        "item_code": '1001',            
        "qty": 10,
        "rate": 100
    })
    # purchase_receipt.set_item(item_code='1001', qty=10, rate=100)  # 设置商品及数量、单价
    purchase_receipt.save()        
    return purchase_receipt
